package com.hspedu.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: xujiabing
 * @date: 2024-11-24 17:27
 * @description: rabbitmq 配置类,可以创建队列，交换机
 */
@Configuration
public class RabbitMQConfig {

    //队列名
    public  static final String QUEUE = "queue";

    //fanout 广播模式
    public  static final String QUEUE1 = "queue_fanout01"; //广播队列1
    public  static final String QUEUE2 = "queue_fanout02"; //广播队列2
    public  static final String EXCHANGE = "fanoutExchange"; //广播模式交换机
    //direct 路由模式
    public static final String QUEUE_DIRECT1 = "queue_direct01";
    public static final String QUEUE_DIRECT2 = "queue_direct02";
    public static final String EXCHANGE_DIRECT = "directExchange";
    public static final String rountingkey01 = "queue.red";
    public static final String rountingkey02 = "queue.green";

    //队列是否持久化
    private static final Boolean durable = true;

    /**
     * 1.配置队列
     * 2.队列名 queue
     * 3.true 表示持久化
     * durable： 队列是否持久化。
     * 队列默认是存放到内存中的，rabbitmq 重启则丢失，若想重启之后还存在则队列要持久化，
     * 保存到Erlang 自带的Mnesia 数据库中，当rabbitmq 重启之后会读取该数据库
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE, durable);
    }

    /**
     * fanout队列1
     * @return
     */
    @Bean
    public Queue queue1() {
        return new Queue(QUEUE1, durable);
    }

    /**
     * fanout队列2
     * @return
     */
    @Bean
    public Queue queue2() {
        return new Queue(QUEUE2, durable);
    }

    /**
     * 创建交换机
     * @return
     */
    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(EXCHANGE);
    }

    /**
     * 绑定队列 queue1(queue_fanout01) 到交换机 exchange (fanoutExchange)
     * @return
     */
    public Binding binding01() {
        return BindingBuilder.bind(queue1()).to(exchange());
    }

    /**
     * 绑定队列 queue2(queue_fanout01) 到交换机 exchange (fanoutExchange)
     * @return
     */
    public Binding binding02() {
        return BindingBuilder.bind(queue2()).to(exchange());
    }

    /**
     * 路由模式队列1
     * @return
     */
    @Bean
    public Queue queue_direct1() {
        return new Queue(QUEUE_DIRECT1);
    }

    /**
     * 路由模式队列2
     * @return
     */
    @Bean
    public Queue queue_direct2() {
        return new Queue(QUEUE_DIRECT2);
    }

    /**
     * 创建Direct 交换机
     * @return
     */
    @Bean
    public DirectExchange exchange_direct() {
        return new DirectExchange(EXCHANGE_DIRECT);
    }

    /**
     * 将队列绑定到指定交换机，并指定路由
     * queue_direct1(): 队列
     * exchange_direct(): 交换机
     * rountingkey01: 路由
     * @return
     */
    @Bean
    public Binding binding_direct1() {
        return BindingBuilder
                .bind(queue_direct1()).to(exchange_direct()).with(rountingkey01);
    }

    /**
     * 将队列绑定到指定交换机，并指定路由
     * queue_direct1(): 队列
     * exchange_direct(): 交换机
     * rountingkey01: 路由
     * @return
     */
    @Bean
    public Binding binding_direct2() {
        return BindingBuilder
                .bind(queue_direct2()).to(exchange_direct()).with(rountingkey02);
    }




}
